package tg.webclient.rj.beans.mtassign;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Vector;

import com.sun.org.apache.xpath.internal.axes.HasPositionalPredChecker;
import com.sun.xml.internal.xsom.impl.scd.Iterators.Map;

import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.mbo.SqlFormat;
import psdi.server.MXServer;
import psdi.util.MXException;
import psdi.webclient.system.beans.DataBean;
import psdi.webclient.system.beans.MultiselectDataBean;
import psdi.webclient.system.controller.AppInstance;
import psdi.webclient.system.controller.Utility;
import psdi.webclient.system.controller.WebClientEvent;

/***
 * 
 * @author pzx
 *
 */
public class MultiSectWTPers extends MultiselectDataBean
{
  private DataBean originalBean;

  /*
   * 
  @Override
  protected MboSetRemote getMboSetRemote() throws MXException,
			RemoteException {
		//MboSetRemote mboset = MXServer.getMXServer().getMboSet("WTASSIGN",sessionContext.getUserInfo());
	  	MboSetRemote mboset=this.getMbo().getMboSet("WTASSIGN");
		if (mboset == null || mboset.isEmpty()) {
			return null;
		}
		String wtassignid=getString("WTASSIGNID");
		SqlFormat sqlf = new SqlFormat(" not exists ( select WTPERSONID from WTASSIGNLINE where WTASSIGNID='"+wtassignid+"' and WTPERSON.WTPERSONID=WTPERSONID   ) ");
		mboset.setWhere(sqlf.format());
		return mboset;
  }
  *
  */
 
  
  public int selectok() throws MXException, RemoteException
  {
    try{
      MboSetRemote mboset = this.app.getDataBean("Mulsel_table").getMboSet();
      MboRemote mbo = this.app.getAppBean().getMbo(0);
      if ((!mboset.isEmpty()) && (mbo.getInt("WTASSIGNID") > 0)) {
        MboSetRemote lineSet = mbo.getMboSet("WTASSIGNLINE");
        HashMap<String,String> keys=new HashMap<String,String>();
        if(lineSet!=null && !lineSet.isEmpty() && lineSet.count()>0){
        	int j=0;
        	for(j=0;j<lineSet.count();j++){
        		MboRemote wmbo = lineSet.getMbo(j);
        		int wtpersonid = wmbo.getInt("WTPERSONID");
        		if(!keys.containsKey(wtpersonid+"")){
        		 	keys.put(wtpersonid+"", wtpersonid+"");
        		}
        	}
        }
        Vector vec = mboset.getSelection();
        if ((vec != null) && (vec.size() > 0)) {
          for (int i = 0; i < vec.size(); i++) {
            MboRemote wtprsmbo = (MboRemote)vec.elementAt(i);
            int wtpersonid = wtprsmbo.getInt("WTPERSONID");
        	if(!keys.containsKey(wtpersonid+"")){
                MboRemote lineMbo = lineSet.add();
                lineMbo.setValue("WTASSIGNID", mbo.getInt("WTASSIGNID"));
                lineMbo.setValue("WTPERSONID", wtpersonid);
        	 	keys.put(wtpersonid+"", wtpersonid+"");
    		}
          }
        }
        lineSet.save();
      }

      this.app.getDataBean("WTPRS_TABLE").reset();
      this.app.getDataBean("WTPRS_TABLE").refreshTable();
      this.app.getDataBean("WTPRS_TABLE").reloadTable();
      this.app.getAppBean().refreshTable();
      this.app.getAppBean().reloadTable();
    }
    catch (RemoteException e)
    {
      e.printStackTrace();
    }
    catch (MXException e) {
      e.printStackTrace();
    }
    Utility.sendEvent(new WebClientEvent("dialogclose", this.app.getCurrentPageId(), null, this.sessionContext));
    return 1;
  }
}